/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spring.modelo.DAO;

import com.spring.modelo.entidad.Planes;
import com.spring.modelo.entidad.Carrera;
import com.spring.modelo.entidad.DetallePlan;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ricardo
 */
@Repository
public class PlanDAO extends HibernateDaoSupport{
    
    @Autowired
    public PlanDAO(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
    
    public List<Planes> getListaPlan(){
        return getHibernateTemplate().find("select p" + " from Planes p");
    }
    
    public Planes buscarPlan(int codigo){
        System.out.println("....."+codigo);
        return getHibernateTemplate().get(Planes.class, codigo);
    }
    
    public boolean insertarPlan(Planes p){
        boolean b=false;
        try{
            getHibernateTemplate().saveOrUpdate(p);
            b=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return b;
    }
    
    public List<Planes> getListaPlanesPorTipo(Carrera c){
        return getHibernateTemplate().findByNamedParam("SELECT p FROM Planes p WHERE p.codCarrera.codCarrera=:codigo", "codigo", c.getCodCarrera());
    }
    
    @Transactional
    public boolean eliminarPlan(int codigo){
        boolean b=false;
        try{
            Planes p=getHibernateTemplate().get(Planes.class,codigo);
            getHibernateTemplate().delete(p);
            b=true;
        }catch(Exception e){
            b=false;
        }
        return b;
    }
    public List<DetallePlan> visualizarPlan(int codigo){
        return getHibernateTemplate().findByNamedParam("SELECT d FROM DetallePlan d WHERE d.detallePlanPK.codPlan=:codigo", "codigo", codigo);
    }
}